package com.fin.zw.intelligentbot.service;

import com.fin.zw.intelligentbot.entity.BaseEntity;
import com.fin.zw.intelligentbot.entity.Company;
import com.fin.zw.intelligentbot.entity.QVariable;
import com.fin.zw.intelligentbot.entity.Variable;
import com.fin.zw.intelligentbot.enums.SourceType;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.querydsl.core.BooleanBuilder;
import org.springframework.stereotype.Service;

@Service("variableService")
public class VariableService extends BaseService<Variable> {

    @Override
    protected BooleanBuilder generateCondition(Company cop,BooleanBuilder builder){

        if (cop != null) {
            builder.and(QVariable.variable.companies.contains(cop));
        } else {
            builder.and(QVariable.variable.source.eq(SourceType.SAFEMANAGER));
        }
        return builder;
    }

    @Override
    protected Variable generateEntity(BaseEntity newEntity,SessionModel sessionModel){
        Variable variable = super.generateEntity(newEntity, sessionModel);
        variable.setId(null);
        variable.setCreaterId(sessionModel.getUserId());
        if (sessionModel.getCompanyId() == null) {//用户为超级管理员，前端传回来的就是当前使用的企业
            variable.setSource(SourceType.SAFEMANAGER);
            variable.setIsShare(true);
        } else {//用户为普通用户，前端传回来的使用企业为空，此处需要补充使用企业为用户所属企业
            variable.setSource(SourceType.COMPANY);
            variable.setCompanies(commonService.getCompanySet(sessionModel, null));
        }
        return variable;
    }


}
